//
//  VIPReginViewController.m
//  1243
//
//  Created by ym on 16/5/9.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "VIPReginViewController.h"
#import "NetWorkTools.h"
#import "RegiseterTwoViewController.h"

@interface VIPReginViewController ()<UITextFieldDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *chongFuCodeText;
@property (weak, nonatomic) IBOutlet UITextField *NameText;
@property (weak, nonatomic) IBOutlet UITextField *shenFZTxt;
@property (weak, nonatomic) IBOutlet UITextField *jiaoYText;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;
@property (nonatomic, strong)NSDictionary *dic;
@property (nonatomic,strong) NSDictionary *loadUserData;
@property (strong, nonatomic) IBOutlet UIView *phoneNum;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UITextField *IDCardText;
@end

@implementation VIPReginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"推荐会员";
    self.NameText.delegate = self;
    self.shenFZTxt.delegate = self;
    self.jiaoYText.delegate = self;
    self.chongFuCodeText.delegate = self;
    self.codeText.delegate = self;
    self.IDCardText.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    [super viewWillAppear:animated];
    NSDictionary *dict = @{
                           @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                           @"Password":[ReadLoginData readLoginData][@"Password"]
                           };
    
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        
        NSDictionary *loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];
        
        [self cleanloginData];
        [ReadLoginData savelogindataToLocal:loginDict];
        
        self.phone.text = [NSString stringWithFormat:@"推荐人手机号码：%@",loginDict[@"UserName"]];
        self.codeText.secureTextEntry = YES;
        self.chongFuCodeText.secureTextEntry = YES;
        self.jiaoYText.secureTextEntry = YES;
        
        if (self.str.length > 0 ) {
            self.phone.text = [NSString stringWithFormat:@"推荐人手机号码：%@",self.str];
            
        }else{
            
        }

        
        self.loadUserData = [ReadLoginData readLoginData];
//        self.phone.text = [NSString stringWithFormat:@"%@", self.loadUserData]

    }];
    
    
}
- (NSDictionary *)loadUserData {
    if (!_loadUserData) {
        _loadUserData = [NSDictionary dictionary];
    }
    return _loadUserData;
    
}
- (void)cleanloginData {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    //    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dic writeToFile:filename atomically:YES];
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    //    NSLog(@"%@", textField.text);
}

// 键盘遮挡
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 1011|| textField.tag == 1010|| textField.tag == 1012) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}
- (IBAction)submitButtonClick:(id)sender {
// 提交按钮
    [self netWork];
    
}
- (void)netWork{
    
    
    
    if (self.phone.text.length == 0 || self.codeText.text.length == 0 || self.jiaoYText.text.length == 0 || self.NameText.text.length == 0 || self.shenFZTxt.text.length == 0 || self.IDCardText.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        
    }else{
        if ([self isMobileNumber:self.phoneNumber.text]) {
            if ([self.codeText.text isEqualToString:self.chongFuCodeText.text]) {
                
                NSString *userName;
            
                
                if (self.str.length > 0 ) {
                    userName = self.str;
                    
                }else{
                      userName = self.loadUserData[@"UserName"];
                    
                }
                
                self.dic= @{
                            //  VNUMBER  自己的
                            @"UserName":self.phoneNumber.text,
                            @"Password":self.codeText.text,
                            @"Buy_Password":self.jiaoYText.text,
                            @"RealName":self.NameText.text,
                            @"Email":self.shenFZTxt.text,
                            //                        @"V_Number":self.loadUserData[@"UserName"],
                            @"V_Name":userName,
                            @"LastLoginTime":@"2016-04-12T15:47:20.6938427+08:00",
                            @"PNumber1":@"1002",
                            @"PName1":@"小兰",
                            @"PNumber2":@"1003",
                            @"PName2":@"小红;",
                            @"PNumber3":@"1004",
                            @"PName3":@"小白",
                            @"UserRole":@"VIP",
                            @"Money":@"0.0000",
                            @"fh_Money":@"0.00",
                            @"gq_Money":@"0.0000",
                            @"gwj_Money":@"0.00",
                            @"Dl_Money":@"0.0000",
                            @"Status":@"0",
                            @"CreateTime":@"2016-04-12T15:47:20.6928426+08:00",
                            @"Remark":@"iOS客户端测试"
                            };
                RegiseterTwoViewController *a = [[RegiseterTwoViewController alloc] initWithNibName:@"RegiseterTwoViewController" bundle:[NSBundle mainBundle]];
                a.str = self.str;
                //            NSMutableDictionary *diction = [NSMutableDictionary dictionaryWithDictionary:self.dic];
                a.dic =self.dic;
                a.type = @"推荐会员";
                a.idcard = self.IDCardText.text;
                [self.navigationController pushViewController:a animated:YES];
                
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您两次输入的密码不一致,请重新设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                
                
                
                
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确格式的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
            
            
        }
     
        
    }
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1111) {
        [self cleanTextField];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self resignFirstResponderTextField];
}

- (void)resignFirstResponderTextField {
    [self.phoneNumber resignFirstResponder];
    [self.codeText resignFirstResponder];
    [self.chongFuCodeText resignFirstResponder];
    [self.NameText resignFirstResponder];
    [self.shenFZTxt resignFirstResponder];
    [self.jiaoYText resignFirstResponder];
    [self.IDCardText resignFirstResponder];
}

- (void)cleanTextField {
    self.phoneNumber.text = @"";
    self.codeText.text = @"";
    self.chongFuCodeText.text = @"";
    self.NameText.text = @"";
    self.shenFZTxt.text = @"";
    self.jiaoYText.text = @"";
    self.IDCardText.text = @"";
}

//判断手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0235-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188, 183
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186 145
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189  181
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
